package com.tb.controller;

import com.tb.dubbo.IUserService;
import com.tb.pojo.User;
import com.tb.utils.IdUtils;
import com.tb.utils.MD5;
import com.tb.utils.Msg;
import com.tb.utils.Utf8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 用户注册
 *
 * @author 何龙
 * @create 2019-01-10 14:22
 */
@RestController
@RequestMapping("/user")
public class RegisterController {

    /**
     * @api {POST} /user/addUsers 普通用户邮箱注册
     * @apiDescription 普通用户注册&nbsp;&nbsp;<span style="color:#D0254E">[*需要在已经发送了邮件验证码基础上请求]</span>
     * <br/><span style="color:#012456"><b>作者:何龙</b></span>
     * @apiGroup User API
     * @apiParam {string} userName 用户名&nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {string} passWord 密码&nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiVersion 1.0.0
     * @apiSuccess {int} code 返回状态码
     * <br/>0:正常 <br/>1:参数有误
     * <br/>3:用户名被占用
     * <br/>5:验证码已失效或者未发送验证码
     * @apiSuccess {string} msg 返回错误信息
     * @apiSuccess {Object} data
     * @apiSuccessExample {json} 正确返回值:
     * {
     * "code": 0,
     * "msg": "",
     * "data": "注册成功"
     * }
     * @apiErrorExample {json} 错误返回值:
     * {
     * "code": 5,
     * "msg": "验证码已失效或者未发送验证码!",
     * "data": null
     * }
     * @apiSampleRequest /user/addUsers
     */

    @Autowired
    private IUserService iUserService;

    @PostMapping("/addUsers")
    public Msg addUsers(@RequestParam(value = "userName", defaultValue = "") String userName,
                        @RequestParam(value = "passWord", defaultValue = "") String passWord,
                        HttpSession session) {
        if ("".equals( userName ) || "".equals( passWord )) {
            return Msg.error( 1, "参数有误" );
        }

        Map<String, String> emailCode1 = (Map<String, String>) session.getAttribute( "emailCode" );
        try {
            String status = emailCode1.get( "status" );
            if (!"ok".equals( status )) {
                return Msg.error( 5, "验证码未通过验证或未发送验证码！" );
            }
        } catch (Exception e) {
            return Msg.error( 5, "验证码未通过验证或未发送验证码！" );
        }


        if (!iUserService.getUserName( Utf8.getString( userName ) )) {
            return Msg.error( 3, "用户名被占用！" );
        }

        User user = new User();
        user.setUserId( IdUtils.genImageName() );
        user.setUserName( userName );
        user.setUserPass( MD5.toMD5( passWord ) );
        user.setUserEmail( emailCode1.get( "email" ) );
        user.setUserStatus( 1 );
        user.setRolerId( 3 );
        if (iUserService.addUser( user )) {
            session.setAttribute("User",user);

            session.removeAttribute("emailCode");
            return Msg.success( "注册成功！" );
        } else {
            return Msg.error( 4, "注册失败！" );
        }


    }
}
